package jgrit.gui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import jgrit.gui.factory.UIFactory;
import jgrit.options.OptionCollection;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MapOptionsPanel extends javax.swing.JPanel {
	/**
	 * Auto-generated serialization version ID
	 */
	private static final long serialVersionUID = 7229306282994216116L;

	private JCheckBox tileReductionFlipOption;
	private JLabel compressionLabel;
	private JLabel offsetLabel;
	private JTextField tileOffsetTextField;
	private JComboBox compressionComboBox;
	private JCheckBox metaTileReductionOption;
	private JComboBox mapLayoutComboBox;
	private JCheckBox tileReductionPaletteOption;
	private JCheckBox tileReductionOption;
	
	/**
	 * Option collection to which the controls on this form are attached
	 */
	private OptionCollection options;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new MapOptionsPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public MapOptionsPanel(OptionCollection options) {
		super();
		this.options = options;
		initGUI();
		setActive(mapLayoutComboBox.getSelectedIndex() != 0);
	}
	
	@Deprecated
	public MapOptionsPanel() {
		this(new OptionCollection());
	}
	
	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout(
					"max(p;5dlu), max(p;5dlu), max(p;5dlu):grow, max(p;5dlu), max(p;5dlu):grow, 5dlu", 
					"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), 5dlu, max(p;5dlu), 5dlu, max(p;5dlu), 5dlu, max(p;5dlu), max(p;5dlu)");
			this.setLayout(thisLayout);
			this.add(getTileReductionFlipOption(), new CellConstraints("3, 6, 3, 1, default, default"));
			this.add(getTileReductionOption(), new CellConstraints("2, 4, 4, 1, default, default"));
			this.add(getTileReductionPaletteOption(), new CellConstraints("3, 5, 3, 1, default, default"));
			this.add(getMapLayoutComboBox(), new CellConstraints("5, 2, 1, 1, default, default"));
			this.add(getMetaTileReductionOption(), new CellConstraints("2, 8, 4, 1, default, default"));

			{
				final JLabel jLabel1 = new JLabel();
				this.add(jLabel1, new CellConstraints("2, 2, 2, 1, default, default"));
				this.add(getCompressionComboBox(), new CellConstraints("5, 10, 1, 1, default, default"));
				this.add(getCompressionLabel(), new CellConstraints("2, 10, 2, 1, default, default"));
				this.add(getOffsetLabel(), new CellConstraints("2, 12, 2, 1, default, default"));
				this.add(getTileOffsetTextField(), new CellConstraints("5, 12, 1, 1, default, default"));
				jLabel1.setText("Map Layout");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JCheckBox getTileReductionOption() {
		if(tileReductionOption == null) {
			tileReductionOption = new JCheckBox();
			tileReductionOption.setText("Tile reduction");
			UIFactory.attachOption(options.mapTileReduction, tileReductionOption);
		}
		return tileReductionOption;
	}
	
	private JCheckBox getTileReductionPaletteOption() {
		if(tileReductionPaletteOption == null) {
			tileReductionPaletteOption = new JCheckBox();
			tileReductionPaletteOption.setText("Palette");
			UIFactory.attachOption(options.mapPalReduction, tileReductionPaletteOption);
		}
		return tileReductionPaletteOption;
	}
	
	private JCheckBox getTileReductionFlipOption() {
		if(tileReductionFlipOption == null) {
			tileReductionFlipOption = new JCheckBox();
			tileReductionFlipOption.setText("Flip");
			UIFactory.attachOption(options.mapFlippedReduction, tileReductionFlipOption);
		}
		return tileReductionFlipOption;
	}
	
	private JComboBox getMapLayoutComboBox() {
		if(mapLayoutComboBox == null) {
			ComboBoxModel mapLayoutComboBoxModel = 
				new DefaultComboBoxModel(
						new String[] { "off", "flat", "sbb", "affine" });
			mapLayoutComboBox = new JComboBox();
			mapLayoutComboBox.setModel(mapLayoutComboBoxModel);
			UIFactory.attachOption(options.mapLayout, mapLayoutComboBox);
			
			mapLayoutComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (mapLayoutComboBox.getSelectedIndex() == 0) { //Off
						setActive(false);
					} else {
						setActive(true);
					}
				}
			});
		}
		return mapLayoutComboBox;
	}
	
	/**
	 * Sets all but the map layout combo box to be editiable in regards to active
	 * @param active - boolean (whether to activate/deactiveate controls
	 */
	protected void setActive(boolean active) {
		for (Component component:this.getComponents()) {
			if (component != mapLayoutComboBox)
				component.setEnabled(active);
		}
	}
	
	private JCheckBox getMetaTileReductionOption() {
		if(metaTileReductionOption == null) {
			metaTileReductionOption = new JCheckBox();
			metaTileReductionOption.setText("Meta-tile palette reduction");
			UIFactory.attachOption(options.metaTilePalReduction, metaTileReductionOption);
		}
		return metaTileReductionOption;
	}
	
	private JComboBox getCompressionComboBox() {
		if(compressionComboBox == null) {
			compressionComboBox = new JComboBox();
			UIFactory.attachOption(options.mapCompression,
					compressionComboBox);
		}
		return compressionComboBox;
	}
	
	private JLabel getCompressionLabel() {
		if(compressionLabel == null) {
			compressionLabel = new JLabel();
			compressionLabel.setText("Compression");
		}
		return compressionLabel;
	}
	
	private JLabel getOffsetLabel() {
		if(offsetLabel == null) {
			offsetLabel = new JLabel();
			offsetLabel.setText("Tile offset");
		}
		return offsetLabel;
	}
	
	private JTextField getTileOffsetTextField() {
		if(tileOffsetTextField == null) {
			tileOffsetTextField = new JTextField();
			UIFactory
					.attachOption(options.tileOffset, tileOffsetTextField);
		}
		return tileOffsetTextField;
	}

}
